package com.gugee.ins.data.task.mark.service;

import com.gugee.ins.common.dal.service.blogger.InsBloggerMainDao;
import com.gugee.ins.common.dal.service.dic.InsDicCategoryDao;
import com.gugee.ins.common.dal.service.dic.InsDicCommercialCategoryDao;
import com.gugee.ins.common.dal.service.dic.InsDicNationDao;
import com.gugee.ins.common.dto.RankTaskParam;
import com.gugee.ins.data.task.config.TaskConfig;
import com.xiguaji.boot.toolkit.DateUtil;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 处理榜单生成的父类
 * @author XiGua
 */
@Slf4j
public abstract class AbstractMarkRankProducerHandler {

    /**
     *
     */
    private static final String EMPTY = "empty";

    /**
     *
     */
    @Autowired
    public InsBloggerMainDao insBloggerMainDao;

    /**
     *
     */
    @Autowired
    InsDicCategoryDao insDicCategoryDao;

    /**
     *
     */
    @Autowired
    InsDicNationDao insDicNationDao;

    /**
     *
     */
    @Autowired
    InsDicCommercialCategoryDao insDicCommercialCategoryDao;

    /**
     *
     */
    @Autowired
    public TaskConfig taskConfig;


    public ReturnT<String> xxlJobStart(String rankName, String param){
        long start = System.currentTimeMillis();
        try {
            log.info(rankName + " 开始");
            XxlJobLogger.log(rankName + " 开始");
            RankTaskParam taskParam = null;
            if(!StringUtils.isEmpty(param)){
                taskParam = JsonUtil.jsonParse(RankTaskParam.class, param);
                if(taskParam == null){
                    log.info(rankName+ " 参数错误,param：{}",param);
                    XxlJobLogger.log(rankName+ " 参数错误,param：{}",param);
                    return ReturnT.FAIL;
                }
            }

            if(taskParam == null){
                taskParam = new RankTaskParam();
            }

            handleRank(taskParam);
            return ReturnT.SUCCESS;
        } catch (Exception ex) {
            XxlJobLogger.log(ex);
            ex.printStackTrace();
            return ReturnT.FAIL;
        }finally {
            log.info(rankName + " 结束, cost total={}", (System.currentTimeMillis() - start) / 1000);
            XxlJobLogger.log(rankName + " 结束, cost total={}",(System.currentTimeMillis() - start) / 1000);
        }
    }

    /**
     * 处理各自的榜单
     * @param taskParam
     */
    public abstract void handleRank(RankTaskParam taskParam);

    /**
     * 获取指定的粉丝量级id信息
     * @param followerSize
     * @return
     */
    public List<Integer> getFollowerSize(int followerSize){
        List<Integer> followerSizeList;
        if(followerSize > 0){
            followerSizeList = insBloggerMainDao.findFollowerSizeIdByLimitId(taskConfig.getInsStBloggerCfsFollowerSizeIdLimit());
        }else{
            followerSizeList = new ArrayList<>(1);
            followerSizeList.add(followerSize);
        }
        return followerSizeList;
    }

    /**
     * 查找一级分类
     * @param category
     * @param addEmpty
     * @return
     */
    public List<String> getCategory(String category,boolean addEmpty){
        List<String> categoryList;
        if(!StringUtils.isEmpty(category)){
            categoryList = new ArrayList<>(1);
            if(EMPTY.equals(category)){
                category = "";
            }
            categoryList.add(category);
        }else{
            categoryList = insDicCategoryDao.findAllOverallCategory();
            if(categoryList == null){
                categoryList = new ArrayList<>(1);
            }
            if(addEmpty){
                categoryList.add("");
            }
        }
        return categoryList;
    }

    /**
     * 查找头部国家
     * @param nationCode
     * @param addEmpty
     * @return
     */
    public List<String> getTopNationCode(String nationCode,boolean addEmpty){
        List<String> nationList;
        if(!StringUtils.isEmpty(nationCode)){
            nationList = new ArrayList<>(1);
            if(EMPTY.equals(nationCode)){
                nationCode = "";
            }
            nationList.add(nationCode);
        }else{
            nationList = insDicNationDao.findTopNationCode();
            if(nationList == null){
                nationList = new ArrayList<>(1);
            }
            if(addEmpty){
                nationList.add("");
            }
        }
        return nationList;
    }

    /**
     * 获取指定时间
     * @param dateTime
     * @return
     */
    public long getDateTime(String dateTime){
        long now;
        if(!StringUtils.isEmpty(dateTime)){
            now = DateUtil.stringToDate(dateTime,"yyyyMMdd").getTime();
        }else{
            now = System.currentTimeMillis();
        }
        return now;
    }

    /**
     * 查找一级分类
     * @param category
     * @param addEmpty
     * @return
     */
    public List<String> getCommercialCategory(String category,boolean addEmpty){
        List<String> categoryList;
        if(!StringUtils.isEmpty(category)){
            categoryList = new ArrayList<>(1);
            if(EMPTY.equals(category)){
                category = "";
            }
            categoryList.add(category);
        }else{
            categoryList = insDicCommercialCategoryDao.findAllOverallCategory();
            if(categoryList == null){
                categoryList = new ArrayList<>(1);
            }
            if(addEmpty){
                categoryList.add("");
            }
        }
        return categoryList;
    }

    /**
     * 判断是否是周一
     * @return
     */
    public boolean isMonday(Calendar calendar){
        if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 判断是否是一号
     * @return
     */
    public boolean isFirstDayOfMonth(Calendar calendar){
        if(calendar.get(Calendar.DAY_OF_MONTH) == 1){
            return true;
        }else {
            return false;
        }
    }
}
